package com.netx.ut.lib.dn;
import java.util.Iterator;
import java.util.Collection;
import java.sql.Timestamp;
import com.netx.generics.R1.util.*;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Extent;
import javax.jdo.Query;
import javax.jdo.JDOHelper;
import javax.jdo.Transaction;



public class NTDataNucleous extends UnitTester {

	public static void main(String[] args) throws Throwable {
		NTDataNucleous nt = new NTDataNucleous();
		nt.test01();
		nt.println("done.");
	}
	
	public void test01() {
        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("datanucleus.properties");
        PersistenceManager pm = pmf.getPersistenceManager();
        // Create product:
        Transaction tx = pm.currentTransaction();
        tx.begin();
        Product product = new Product();
        product.setProductId("P1000");
        product.setName("Walkman");
        product.setPrice(105.55);
        product.setTimeCreated(new Timestamp(10000L));
        pm.makePersistent(product);
        tx.commit();
        // Retrieve all products:
        println("All products:");
        tx = pm.currentTransaction();
        tx.begin();
        Extent<Product> e1 = pm.getExtent(Product.class, true);
        Iterator<Product> iter = e1.iterator();
        while(iter.hasNext()) {
            Product p = iter.next();
            println("Product: "+p.getName());
        }
        tx.commit();
        // Retrieve all products with price smaller than 100:
        println("Products with price smaller than 100:");
        tx = pm.currentTransaction();
        tx.begin();
        Extent<Product> e2 = pm.getExtent(Product.class, true);
        Query q = pm.newQuery(e2, "price < 100.00");
        q.setOrdering("price ascending");
        @SuppressWarnings("unchecked")
		Collection<Product> c = (Collection<Product>)q.execute();
        iter = c.iterator();
        while(iter.hasNext()) {
            Product p = iter.next();
            println("Product: "+p.getName());
        }
        tx.commit();
        pm.close();
 	}
}
